<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="telephone=no" name="format-detection">
    <meta content="email=no" name="format-detection">
    <title>酷数 - 微信登录</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="/static/css/main.css">
    <link rel="stylesheet" href="/static/element-ui/css/index.css">
    <style>
        .signin {
            height: 100vh;
            background-color: #f5f5f5;
        }

        .signin .signin-content {
            width: 480px;
            height: 540px;
            border-radius: 6px;
            left: 50%;
            top: 50px;
            margin-bottom: 50px;
            -webkit-transform: translateX(-50%);
            transform: translateX(-50%);
            background-color: #fff;
            position: absolute;
        }

        .signin .heade {
            padding-top: 82px;
        }

        .signin .heade .logo {
            height: 80px;
            display: block;
            margin: 0 auto;
        }

        .signin .wechatSignin {
            width: 272px;
            height: 272px;
            margin: 0 auto;
            border-radius: 4px;
            border: 1px solid #dedfe1;
        }

        .signin .wechatSignin .qrCode {
            width: 185px;
            height: 185px;
            padding-top: 25px;
            margin: 0 auto;
        }

        .signin .wechatSignin .qrCode .qrCodeImg {
            display: block;
            width: 185px;
            height: 185px;
        }

        .signin .wechatSignin .qrCode .qrCodeReload {
            left: 147px;
            top: 188px;
            display: block;
            width: 185px;
            height: 185px;
            position: absolute;
            cursor: pointer;
        }

        .signin .wechatSignin .tips {
            line-height: 30px;
            margin-top: 4px;
            font-size: 14px;
            color: #404040;
            text-align: center;
        }
    </style>
</head>
<body>
<div id="app">
    <div v-if="!isWx" class="signin pr">
        <div class="signin-content">
            <div class="heade pr">
                <a href="/">
                    <img src="/static/images/logo.png" class="logo"></a>
            </div>
            <div class="wechatSignin">
                <div class="qrCode pr">
                    <img v-if="isExpires" src="/static/images/qrcode_reload.png" class="qrCodeReload"
                         @click="get_qrcode">
                    <img :src="qr_code" class="qrCodeImg">
                </div>
                <div class="tips">
                    <span class="iconfont icon-weixin pr"></span>
                    扫码关注「酷数」快速登录
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 解决IE兼容性 -->
<script src="/static/js/url-search-params-polyfill.js"></script>
<script src="/static/js/browser-polyfill.js"></script>
<script>
    if (!window.Promise) {
        window.Promise = Promise;
    }
</script>

<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="/static/js/vue.js"></script>
<script src="/static/element-ui/js/index.js"></script>
<script src="/static/js/axios.min.js"></script>


<script>
    // 以分钟为单位，设置localStorage过期时间，用于存储服务器数据
    // set参数expire可不传，setItem到期时间标识expire会标识为-1,get时无过期时间，直接返回set中的val
    let localStorageUtil = {
        set: function (key, val, expire) {
            let exp = expire ? Date.now() + expire * 1000 * 60 : -1;
            localStorage.setItem(key, JSON.stringify({value: val, expire: exp}));
            console.log('set ' + key + ':' + JSON.stringify({value: val, expire: new Date(exp).toLocaleString()}));
        },
        get: function (key) {
            let data = localStorage.getItem(key);
            console.log(data);
            if (!data) return null;
            let dataObj = JSON.parse(data);
            if (dataObj.expire === -1) return dataObj.value;
            if (Date.now() >= dataObj.expire) {
                localStorage.removeItem(key);
                return null;
            } else {
                return dataObj.value;
            }
        }
    };

    new Vue({
        el: "#app",
        data: {
            qr_code: "",
            expires: 0,
            scene_id: "",
            isExpires: false
        },
        mounted: function () {
            this.expires = parseInt(parseInt(new Date()) / 1000);
            if (this.isWx) this.wx_auth();
            else {
                this.get_qrcode();
                setInterval(this.jwt_verify, 1000);
                setInterval(this.isCodeExpires, 5000);
            }
        },
        methods: {
            isCodeExpires: function () {
                let now = parseInt(parseInt(new Date()) / 1000);
                this.isExpires = now > this.expires;
            },
            wx_auth: function () {
                axios.get("/user/get_authorize_url/").then(function (response) {
                    let res = response.data;
                    window.location.href = res.authorize_url;
                })
            },
            get_qrcode: function () {
                let _this = this;
                axios.get("/user/get_qrcode/").then(function (response) {
                    let res = response.data;
                    _this.qr_code = res.qr_code;
                    _this.scene_id = res.scene_id;
                    _this.expires = res.expires;
                    _this.isExpires = false;
                })
            },
            jwt_verify: function () {
                let _this = this;
                axios.get("/user/jwt_verify/", {params: {scene_id: this.scene_id}}).then(function (response) {
                    let res = response.data;
                    if (res.status === "success") {
                        _this.token = res.token;
                        // window.localStorage.setItem("token", res.token);
                        localStorageUtil.set("token", res.token, 60);
                        _this.$message.success("登录成功，正在跳转，请稍等！");
                        window.location.href = "/?timestamp=" + new Date().getTime() + Math.random();
                    }
                })
            }
        },
        computed: {
            isWx: function () {
                var ua = navigator.userAgent.toLowerCase();
                return !!ua.match(/micromessenger/i);
            }
        }
    })
</script>
</body>
</html>
